package com.splic.product.service.impl;

import java.util.Date;
import java.util.List;
import com.splic.common.utils.DateUtils;
import com.splic.product.dto.ExchangeRateRequestDto;
import com.splic.product.service.impl.thirdRequest.ExchangeRateRequestServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.splic.product.mapper.ExchangeRateMapper;
import com.splic.product.domain.ExchangeRate;
import com.splic.product.service.ExchangeRateService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 汇率Service业务层处理
 * 
 * @author singe
 * @date 2022-01-04
 */
@Service
public class ExchangeRateServiceImpl implements ExchangeRateService
{
    @Autowired
    private ExchangeRateMapper exchangeRateMapper;
    @Autowired
    private ExchangeRateRequestServiceImpl exchangeRateRequestServiceImpl;

    /**
     * 查询汇率
     * 
     * @param id 汇率主键
     * @return 汇率
     */
    @Override
    public ExchangeRate selectExchangeRateById(Long id)
    {
        return exchangeRateMapper.selectExchangeRateById(id);
    }

    /**
     * 查询汇率列表
     * 
     * @param exchangeRate 汇率
     * @return 汇率
     */
    @Override
    public List<ExchangeRate> selectPageList(ExchangeRate exchangeRate)
    {
        return exchangeRateMapper.selectExchangeRateList(exchangeRate);
    }

    /**
     * 查询汇率列表
     *
     * @param exchangeRate 汇率
     * @return 汇率
     */
    @Override
    public List<ExchangeRate> selectExchangeRateList(ExchangeRate exchangeRate)
    {
        return exchangeRateMapper.selectExchangeRateList(exchangeRate);
    }

    /**
     * 新增汇率
     * 
     * @param exchangeRate 汇率
     * @return 结果
     */
    @Override
    public int insert(ExchangeRate exchangeRate)
    {
        exchangeRate.setCreateTime(DateUtils.getNowDate());
        return exchangeRateMapper.insert(exchangeRate);
    }

    /**
     * 修改汇率
     * 
     * @param exchangeRate 汇率
     * @return 结果
     */
    @Override
    public int update(ExchangeRate exchangeRate)
    {
        exchangeRate.setUpdateTime(DateUtils.getNowDate());
        return exchangeRateMapper.update(exchangeRate);
    }


    /**
     * 删除汇率信息
     * 
     * @param id 汇率主键
     * @return 结果
     */
    @Override
    public int deleteById(Long id)
    {
        return exchangeRateMapper.deleteById(id);
    }
    /**
     * 修改汇率
     *
     * @param exchangeRate 汇率信息
     * @return 结果
     */
    @Override
    @Transactional
    public int saveOrUpdate(ExchangeRate exchangeRate)
    {
        if(exchangeRate.getId() != null){
            exchangeRate.setUpdateTime(new Date());
            return exchangeRateMapper.update(exchangeRate);
        }else{
            exchangeRate.setDate(new Date());
            exchangeRate.setCreateTime(new Date());
            return exchangeRateMapper.insert(exchangeRate);
        }
    }

    public void requestThirdData(){
        ExchangeRateRequestDto dto = exchangeRateRequestServiceImpl.requestThirdData();
        if (dto != null && CollectionUtils.isNotEmpty(dto.getExchangeRateList())){
            Date currentDate = DateUtils.getCurrentDayStartTime();
            for(ExchangeRate exchangeRate:dto.getExchangeRateList()){
                ExchangeRate exchangeRateQuery = new ExchangeRate();
                exchangeRateQuery.setDate(currentDate);
                exchangeRateQuery.setTcur(exchangeRate.getTcur());
                exchangeRateQuery.setScur(exchangeRate.getScur());
                List<ExchangeRate> exchangeRateList = exchangeRateMapper.selectExchangeRateList(exchangeRateQuery);
                if(CollectionUtils.isNotEmpty(exchangeRateList)){
                    exchangeRate.setId(exchangeRateList.get(0).getId());
                }
                saveOrUpdate(exchangeRate);
            }
        }
    }
}
